#!/bin/bash
# Copyright 2019 The Vitess Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Required version of golangci-lint
REQUIRED_VERSION="v2.1.6" # https://github.com/golangci/golangci-lint/releases/tag/v2.1.6

# Function to compare versions in pure Bash
version_greater_or_equal() {
  local IFS=.
  local i
  local ver1=($1)
  local ver2=($2)

  # Fill empty fields in ver1 with zeros
  for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)); do
    ver1[i]=0
  done
  # Fill empty fields in ver2 with zeros
  for ((i=${#ver2[@]}; i<${#ver1[@]}; i++)); do
    ver2[i]=0
  done

  for ((i=0; i<${#ver1[@]}; i++)); do
    if ((10#${ver1[i]} > 10#${ver2[i]})); then
      return 0
    elif ((10#${ver1[i]} < 10#${ver2[i]})); then
      return 1
    fi
  done
  return 0
}

# Check if golangci-lint is installed and capture the version
if ! command -v golangci-lint >/dev/null 2>&1; then
  echo "golangci-lint not found. Installing version $REQUIRED_VERSION..."
  go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$REQUIRED_VERSION
else
  VERSION_OUTPUT=$(golangci-lint --version)
  INSTALLED_VERSION=$(echo "$VERSION_OUTPUT" | sed -E -n 's/^golangci-lint has version v?([0-9.]+) .*/\1/p')
  if ! version_greater_or_equal "$INSTALLED_VERSION" "${REQUIRED_VERSION#v}"; then
    echo "golangci-lint version $INSTALLED_VERSION found, but $REQUIRED_VERSION or newer is required."
    echo "Installing version $REQUIRED_VERSION..."
    go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$REQUIRED_VERSION
  fi
fi

# Get list of Go files to lint
gofiles=$(git diff --cached --name-only --diff-filter=ACM | grep '^go/.*\.go$')
if [ -z "$gofiles" ]; then
  exit 0
fi

# Get unique directories of the Go files
gopackages=$(echo "$gofiles" | xargs -n1 dirname | sort -u | paste -sd ' ' -)

# Lint the Go packages
echo "Linting $gopackages"
golangci-lint run $gopackages
